--==========================================
-- 文件删除 freebytes_delete.lua
--==========================================

local upload = require "resty.upload"
local cjson = require "cjson"

-- 获取http请求的所有参数
local args = ngx.req.get_uri_args()
if not args then
    ngx.exit(ngx.HTTP_BAD_REQUEST)
end

local filename = args["filename"] or "noname.file"
local billingcode = args["billingcode"] or ""
local filetype = args["type"]

local response = {["code"] = 200, ["msg"] = "remove success!"}

-- 保存文件根目录
local save_file_root = ngx.var.store_dir

-- 确定删除文件路径
local remove_file_path = save_file_root

remove_file  = remove_file_path .. "/" .. filename

-- 判断删除文件是否存在
local dfile = io.open(remove_file, "rb")
if dfile then
    dfile:close()
else
    response.code = 403
    response.msg = "the remove file is not exist!"
    ngx.say(cjson.encode(response))
    return
end

-- 执行删除
local res, err = os.remove(remove_file)
if not res then
    response.code = 404
    response.msg = "failed to remove " .. remove_file .. ", err: " .. (err or '')
else
   ngx.log(ngx.ERR, "success to remove file: " .. remove_file)
end

ngx.say(cjson.encode(response))
